package Sequence;

import java.util.Arrays;

public class MySequence implements ISequence {

    public int[] elem;
    public int size;

    public MySequence() {
        this.elem = new int[10];
    }


    @Override
    public void display() {
        for (int i = 0; i < size; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }

    @Override
    public boolean isFull() {
        return size == 0;
    }

    @Override
    public void add(int data) {
        if (isFull()) {
            elem = Arrays.copyOf(elem, 2 * elem.length);
        }
        elem[size] = data;
        size++;
    }

    //见擦汗输入add的index是否合法
    public void checkAddIndex(int index) throws PosNotLegalException {
        if (index < 0 || index > size) {
            throw new PosNotLegalException("pos值不合法");
        }
    }

    @Override
    public void add(int pos, int data) {
        if (isFull()) {
            elem = Arrays.copyOf(elem, 2 * elem.length);
        }
        try {
            checkAddIndex(pos);
        } catch (PosNotLegalException e) {
            e.printStackTrace();
        }
        for (int i = size - 1; i > pos - 1; i--) {
            elem[i + 1] = elem[i];
        }
        elem[pos] = data;
        size++;
    }


    @Override
    public boolean contain(int toFind) {
        for (int i = 0; i < size; i++) {
            if (elem[i] == toFind) return true;
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < size; i++) {
            if (elem[i] == toFind) return i;
        }
        return -1;
    }


    public void checkGetAndSetPos(int pos) throws PosNotLegalException {
        if (pos < 0 || pos > size) {
            throw new PosNotLegalException("pos位置不合法");
        }
    }

    @Override
    public int get(int pos) {
        try {
            checkGetAndSetPos(pos);
        } catch (PosNotLegalException e) {
            e.printStackTrace();
        }
        return elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        try {
            checkGetAndSetPos(pos);
        } catch (PosNotLegalException e) {
            e.printStackTrace();
        }
        elem[pos] = value;
    }

    @Override
    public void remove(int key) {
        for (int i = 0; i < size; i++) {
            if (elem[i] == key) {
                for (int j = i; j < size - 1; j++) {
                    elem[j] = elem[j + 1];
                }
                this.size--;
                return;
            }
        }
        System.out.println("没有要删除的元素");
    }

    public void remove2(int key) {
        int pos = indexOf(key);
        if (pos == -1){
            System.out.println("没有要删除的元素");
            return;
        }
        for (int i = pos; i < size-1; i++) {
            elem[i] = elem[i+1];
        }
        size--;
    }

    @Override
    public void removeAll(int key) {
        int flag = -1;
        for (int i = 0; i < size; i++) {
            if (elem[i] == key) {
                flag = 1;
                for (int j = i; j < size - 1; j++) {
                    elem[j] = elem[j + 1];
                }
                this.size--;
                i = 0;
            }
        }
        if(flag == -1){
            System.out.println("没有要删的元素");
        }
    }

    public void removeAll2(int key) {
        int flag = -1;
        for (int i = 0; i < size-1; i++) {
            int pos = indexOf(key);
            if(pos != -1){
                flag = 1;
                for (int j = pos; j < size-1; j++) {
                    elem[j] = elem[j+1];
                }
                size--;
            }
        }
        if(flag == -1){
            System.out.println("没有要删除的元素");
        }
    }

    @Override
    public void clear() {
        size = 0;
    }
}
